package com.ouyy.volunteerservice.volunteer.controller;

import com.ouyy.volunteerservice.volunteer.domain.po.VolunteerActivities;
import com.ouyy.volunteerservice.volunteer.domain.po.VolunteerRegistration;
import com.ouyy.volunteerservice.volunteer.service.VolunteerActivateService;
import com.ouyy.volunteerservice.volunteer.service.VolunteerRegistrationService;
import com.ouyy.volunteerservice.volunteer.service.VolunteerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/registrations")
public class VolunteerRegistrationController {

    @Autowired
    private VolunteerActivateService activateService;

    @Autowired
    private VolunteerRegistrationService registrationService;

    @Autowired
    private VolunteerService volunteerService;

    // 获取所有报名信息
    @GetMapping
    public List<VolunteerRegistration> getAllRegistrations() {
        return registrationService.list();
    }

    // 根据报名ID查询报名信息
    @GetMapping("/{id}")
    public VolunteerRegistration getRegistrationById(@PathVariable Integer id) {
        return registrationService.getById(id);
    }

    // 新增报名信息
    @PostMapping
    public boolean createRegistration(@RequestBody VolunteerRegistration registration) {
        registration.setRegistrationId((int) (UUID.randomUUID().getLeastSignificantBits() & Integer.MAX_VALUE));
        VolunteerActivities activate = activateService.getById(registration.getActivityId());
        String registrationScope = activate.getRegistrationScope();
        Integer organizationId = activate.getOrganizationId();
        Integer volunteerId = registration.getVolunteerId();
        if("2".equals(registrationScope)){
            String organizationIds = volunteerService.getById(volunteerId).getOrganizationIds();
            String phoneNumber = volunteerService.getById(volunteerId).getPhoneNumber();
            registration.setContactPhone(phoneNumber);
            if (organizationIds.contains(organizationId.toString())){
                return registrationService.save(registration);
            }else {
                return false;
            }
        }else{
            String phoneNumber = volunteerService.getById(volunteerId).getPhoneNumber();
            registration.setContactPhone(phoneNumber);
            return registrationService.save(registration);
        }
    }

    // 更新报名信息
    @PutMapping("/{id}")
    public boolean updateRegistration(@PathVariable Integer id, @RequestBody VolunteerRegistration registration) {
        registration.setRegistrationId(id);
        return registrationService.updateById(registration);
    }

    // 删除报名信息
    @DeleteMapping("/{id}")
    public boolean deleteRegistration(@PathVariable Integer id) {
        return registrationService.removeById(id);
    }

    // 根据活动ID删除报名信息
    @DeleteMapping("/activity/{activityId}")
    public boolean deleteByActivityId(@PathVariable Integer activityId) {
        return registrationService.removeById(activityId);
    }

    // 根据志愿者ID删除报名信息
    @DeleteMapping("/volunteer/{volunteerId}")
    public boolean deleteByVolunteerId(@PathVariable Integer volunteerId) {
        return registrationService.removeById(volunteerId);
    }

    // 根据活动ID和志愿者ID查询报名信息
    @GetMapping("/check")
    public VolunteerRegistration checkRegistration(@RequestParam Integer activityId, @RequestParam Integer volunteerId) {
        return registrationService.findByActivityIdAndVolunteerId(activityId, volunteerId);
    }


    // 根据志愿者ID查询报名信息
    @GetMapping("/checkByVolunteerId")
    public ResponseEntity<List<VolunteerRegistration>> checkRegistrationByVid(@RequestParam Integer volunteerId) {
        log.info("Received request to check registration by volunteerId: " + volunteerId);

        if (volunteerId == null) {
            return ResponseEntity.badRequest().body(Collections.emptyList()); // 返回 400 状态码，避免 NullPointerException
        }

        List<VolunteerRegistration> registrations = registrationService.findByVolunteerId(volunteerId);

        if (registrations.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(registrations); // 返回 204 状态码，表示没有数据
        }
        registrations.forEach(registration -> {
            Integer activityId = registration.getActivityId();
            VolunteerActivities activateServiceById = activateService.getById(activityId);
            registration.setActivityName(activateServiceById.getActivityName());
        });

        return ResponseEntity.ok(registrations); // 200 状态码，返回数据
    }

}
